package com.homework.controller;

import com.homework.dto.ClassScheduleDTO;
import com.homework.dto.Result;
import com.homework.entity.ClassSchedule;
import com.homework.service.ClassScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/schedule")
@RequiredArgsConstructor
public class ScheduleController {

    private final ClassScheduleService classScheduleService;

    /**
     * 获取学生的完整课程表
     */
    @GetMapping("/class-schedule")
    public Result<List<ClassSchedule>> getClassSchedule(@RequestParam Long studentId) {
        return classScheduleService.getClassSchedule(studentId);
    }

    /**
     * 获取明天的课程
     */
    @GetMapping("/tomorrow-classes")
    public Result<List<ClassSchedule>> getTomorrowClasses(@RequestParam Long studentId) {
        return classScheduleService.getTomorrowClasses(studentId);
    }

    /**
     * 更新课程表单项（需要管理员权限）
     */
    @PutMapping("/class-schedule/{id}")
    public Result<String> updateClassSchedule(@PathVariable Long id, @Valid @RequestBody ClassScheduleDTO dto) {
        dto.setId(id);
        return classScheduleService.updateClassSchedule(id, dto);
    }

    /**
     * 批量更新课程表（需要管理员权限）
     */
    @PostMapping("/class-schedule/batch")
    public Result<String> batchUpdateSchedule(@Valid @RequestBody BatchUpdateRequest request) {
        return classScheduleService.batchUpdateSchedule(request.getStudentId(), request.getScheduleList());
    }

    /**
     * 删除课程（需要管理员权限）
     */
    @DeleteMapping("/class-schedule/{id}")
    public Result<String> deleteClassSchedule(@PathVariable Long id) {
        return classScheduleService.deleteClassSchedule(id);
    }

    /**
     * 批量更新请求对象
     */
    public static class BatchUpdateRequest {
        private Long studentId;
        private List<ClassScheduleDTO> scheduleList;

        public Long getStudentId() {
            return studentId;
        }

        public void setStudentId(Long studentId) {
            this.studentId = studentId;
        }

        public List<ClassScheduleDTO> getScheduleList() {
            return scheduleList;
        }

        public void setScheduleList(List<ClassScheduleDTO> scheduleList) {
            this.scheduleList = scheduleList;
        }
    }
}